﻿using System;

namespace HW_2
{
    // 事件参数类
    public class BigMoneyArgs : EventArgs
    {
        //get属性,只读
        public string AccountNumber { get; }
        public decimal Amount { get; }

        public BigMoneyArgs(string accountNumber, decimal amount)
        {
            AccountNumber = accountNumber;
            Amount = amount;
        }
    }

    // 定义自定义异常类
    public class BadCashException : Exception
    {
        public BadCashException(string message) : base(message) { }

    }

    // 定义账号类
    public class Account
    {
        public string AccountNumber { get; }
        public decimal Balance { get; protected set; }

        public Account(string accountNumber, decimal initialBalance)
        {
            AccountNumber = accountNumber;
            Balance = initialBalance;
        }

        public virtual void Deposit(decimal amount)
        {
            //不用加this.也行

            Balance += amount;
        }

        public virtual void Withdraw(decimal amount)
        {
            if (amount > Balance)
                throw new InvalidOperationException("Insufficient funds.");
            Balance -= amount;
        }
    }

    // 定义信用账号类，继承自账号类
    public class CreditAccount : Account
    {
        public decimal CreditLimit { get; }

        public CreditAccount(string accountNumber, decimal initialBalance, decimal creditLimit)
            : base(accountNumber, initialBalance)
        {
            CreditLimit = creditLimit;
        }

        public override void Withdraw(decimal amount)
        {
            if (amount > Balance + CreditLimit)
                throw new InvalidOperationException("Credit limit exceeded.");
            Balance -= amount;
        }
    }

    // 定义ATM类
    public class ATM
    {
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        public void Withdraw(Account account, decimal amount)
        {
            account.Withdraw(amount);

            if (amount > 10000)
                OnBigMoneyFetched(new BigMoneyArgs(account.AccountNumber, amount));
        }

        protected virtual void OnBigMoneyFetched(BigMoneyArgs e)
        {
            BigMoneyFetched?.Invoke(this, e);
        }
    }

    class Bank
    {
        static void Main(string[] args)
        {
            Account account = new Account("123456", 50000);
            CreditAccount creditAccount = new CreditAccount("654321", 20000, 10000);
            ATM atm = new ATM();

            //+=是事件订阅操作符，用于将一个事件处理程序附加到事件上。
            atm.BigMoneyFetched += (sender, e) =>
            {
                Console.WriteLine($"Alert: Big money fetched! Account: {e.AccountNumber}, Amount: {e.Amount}");
            };

            try
            {
                atm.Withdraw(account, 15000);
                atm.Withdraw(creditAccount, 25000);
            }
            catch (BadCashException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
